﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Service;
using Agresso.Interface.Layer.Gateway;
using System.Runtime.InteropServices;
using Agresso.Interface.Layer.Domain;

namespace Agresso.Interface.Layer.WinServiceTest
{
    class Program
    {
        private static System.Timers.Timer aTimer;
        private static ConfigurationSettingService m_confService;
        private static VesselService m_vesselService;
        private static CommissionInvoiceService m_CommissionInvoiceService;
        private static BunkerPOService m_BunkerPOService;
        private static BunkerGRNService m_BunkerGRNService;
        delegate void Job();
        private static void ExecuteJob(ConfigurationSetting cs, Job ex)
        {
            DateTime startDate = Convert.ToDateTime(cs.val_4);
            int everyD = Convert.ToInt16(cs.val_3);
            bool isEndDate = Convert.ToBoolean(cs.val_5);
            DateTime endDate = Convert.ToDateTime(cs.val_6);
            bool isRepeatTask = Convert.ToBoolean(cs.val_7);
            int everyTask = Convert.ToInt16(cs.val_8);
            int durationTask = Convert.ToInt16(cs.val_9);

            string timenow = DateTime.Now.Hour + ":" + DateTime.Now.Minute;
            string[] executeTime = cs.val_2.Split(':');
            DateTime executeDateTime = DateTime.Today.AddHours(Convert.ToInt16(executeTime[0])).AddMinutes(Convert.ToInt16(executeTime[1]));
            string executeTimeRepeat = "";

            if (DateTime.Today.Date.Subtract(startDate.Date).Days % everyD == 0)
            {
                if (isEndDate)
                {
                    if (DateTime.Today.Date <= endDate.Date)
                    {
                        if (timenow == cs.val_2)
                        {
                            Console.WriteLine(string.Format("The Update event at {0}", DateTime.Now));
                            ex();
                        }
                        else
                        {
                            if (isRepeatTask)
                            {
                                int times = durationTask / everyTask;
                                for (int i = 1; i <= times; i++)
                                {
                                    executeDateTime = executeDateTime.AddMinutes(everyTask);
                                    executeTimeRepeat = executeDateTime.Hour + ":" + executeDateTime.Minute;
                                    if (executeTimeRepeat == timenow)
                                    {
                                        Console.WriteLine(string.Format("The Update event at {0}", DateTime.Now));
                                        ex();
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (timenow == cs.val_2)
                    {
                        Console.WriteLine(string.Format("The Update event at {0}", DateTime.Now));
                        ex();
                    }
                    else
                    {
                        if (isRepeatTask)
                        {
                            int times = durationTask / everyTask;
                            for (int i = 1; i <= times; i++)
                            {
                                executeDateTime = executeDateTime.AddMinutes(everyTask);
                                executeTimeRepeat = executeDateTime.Hour + ":" + executeDateTime.Minute;
                                if (executeTimeRepeat == timenow)
                                {
                                    Console.WriteLine(string.Format("The Update event at {0}", DateTime.Now));
                                    ex();
                                }
                            }
                        }
                    }
                }
            }
        }
        static void MainX(string[] args)
        {
            //ServiceInstaller.Uninstall("AgressoInterfaceLayerWinService");
            //ServiceInstaller.InstallAndStart("AgressoInterfaceLayerWinService", "AgressoInterfaceLayerWinService", @"C:\Project\Agresso.Interface.Layer\Agresso.Interface.Layer.WinService\bin\Debug\Agresso.Interface.Layer.WinService.exe");
            // ServiceInstaller.StartService("AgressoInterfaceLayerWinService");

            //DataBaseConnectivity dbCon = new DataBaseConnectivity();
            //VesselService vs = new VesselService(dbCon);
            //vs.ValidateVesselMaster();
            //CommissionInvoiceService cmi = new CommissionInvoiceService();
            //cmi.ExecuteCommissionInvoiceTCReletType();
            //cmi.ExecuteCommissionInvoiceVCReletType();
            //cmi.ExecuteFreightHireInvoice();
            //System.Threading.Thread.Sleep(5000);
            //BunkerPOService bs = new BunkerPOService();
            //bs.ExecuteBunkerProcurementPO();

            // BunkerGRNService grn = new BunkerGRNService();
            // grn.ExecuteBunkerGRN();
            //try
            //{
            //    ConfigurationSettingService m_confService = new ConfigurationSettingService();
            //    ConfigurationSetting cs = m_confService.GetConfigurationSetting("ScheduleService");
            //    ExecuteJob(cs, new Job(ExecuteInterface));
            //}
            //catch (Exception x)
            //{
            //    Console.WriteLine(x.Message);
            //}
            //System.Threading.Thread.Sleep(1000);


            aTimer = new System.Timers.Timer(60000);
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
            aTimer.Enabled = true;
            Console.ReadLine();
            //while (true)
            //{
            //}
        }

        static void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                ConfigurationSettingService m_confService = new ConfigurationSettingService();
                ConfigurationSetting cs = m_confService.GetConfigurationSetting("TCRelet");
                Console.WriteLine(DateTime.Now.ToLongTimeString());
                ExecuteJob(cs, new Job(ExecuteInterface));
            }
            catch (Exception x)
            {
                Console.WriteLine(x.Message);
            }
        }
        private static void initService()
        {
            m_confService = new ConfigurationSettingService();
            m_vesselService = new VesselService();
            m_CommissionInvoiceService = new CommissionInvoiceService();
            m_BunkerPOService = new BunkerPOService();
            m_BunkerGRNService = new BunkerGRNService();
        }
        private static void ExecuteInterface()
        {
            initService();
            m_vesselService.ValidateVesselMaster();
            m_CommissionInvoiceService.ExecuteCommissionInvoiceTCReletType();
            m_CommissionInvoiceService.ExecuteCommissionInvoiceVCReletType();
            m_CommissionInvoiceService.ExecuteFreightHireInvoice();
            m_BunkerPOService.ExecuteBunkerProcurementPO();
            m_BunkerGRNService.ExecuteBunkerGRN();
        }
    }
}
